@echo off
rem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
rem  Package:		OurGrid 4.0
rem  Description:	This batch script is used to kill a Virtual Box 
rem                 virtual machine. It makes use of the VBOX_OPTS file
rem 				to determine the shared folder�s name.
rem  Copyright (c)  2007 Universidade Federal de Campina Grande 
rem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

if %1.==/?. goto usage
if %2.==. goto usage
goto findself

:findself
set SELF=%~d0%~p0
set SHARED=ourgridshared
goto main

:main
set VMANAGE=%1
set IMAGE=%2

%VMANAGE% controlvm %IMAGE% poweroff

:loop
%VMANAGE% showvminfo %IMAGE% | find "running"
if %ERRORLEVEL% == 0 (
	call "%SELF%\sleep.bat" 2
	goto loop
)

%VMANAGE% sharedfolder remove %IMAGE% -name %SHARED%
if not %ERRORLEVEL% == 0 (
	echo Unable to remove shared folder. This will have implications on future executions 1>&2
)
goto eof

:usage
echo usage: 1>&2
echo [CALL] %0 ^<VMANAGE COMMAND^> ^<VBOX IMAGE NAME^> 1>&2

:eof